<?php
class categoryModel extends hopeCmsModel
{

	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
    {
        return '{category}';
    }

    public function primaryKey()
    {
        return 'cat_id';
    }

    public function getCats($site_id = 1)
    {    
        $cache = D::app()->cache()->setCachePath(date('Ym'));
        if (!($data = $cache->get('site_id_'.$site_id.'_tree'))) {
            $data = $this->findsByAttribute(
                'cat_id, model_id, parent_id, sort, type, is_show, name, enname',
                'site_id='.$site_id
            );
            $cache->set('site_id_'.$site_id.'_tree', $data, 0);
        }
        return $data;
    }

    public function typeOne()
    {
        $_POST['name']   = trim(htmlspecialchars($_POST['name']));
        $_POST['enname'] = trim(htmlspecialchars($_POST['enname']));

        $_POST['seo_title'] = trim(htmlspecialchars($_POST['seo_title']));
        $_POST['seo_keywords'] = trim(htmlspecialchars($_POST['seo_keywords']));
        $_POST['seo_description'] = trim(htmlspecialchars($_POST['seo_description']));

        if (empty($_POST['enname'])) {
            $py = new dpPinyin;
            $_POST['enname'] = $py->output($_POST['name']);
            unseT($py);
        }

        if (!empty($_POST['image'])) {
            $_POST['image'] = substr($_POST['image'], strlen(ROOT) - 1);
        }
        $this->setData($_POST);
        return $this;
    }

    public function typeTwo()
    {
        $_POST['name']   = trim(htmlspecialchars($_POST['name']));
        if (!empty($_POST['image'])) {
            $_POST['image'] = substr($_POST['image'], strlen(ROOT) - 1);
        }
        $this->setData($_POST);
        return $this;
    }

    /**
     * 重载保存后置操作
     */
    protected function afterSave()
    {
        $site_id = $_SESSION['admin_site_id'];
        $cache   = D::app()->cache();
        $cache->setCachePath(date('Ym'))->delete('site_id_'.$site_id.'_tree');
        $cache->setCachePath(date('Ym'))->delete('site_id_all_'.$site_id.'_tree');
    }

    /**
     * 获取option树
     * @param string $name name属性值
     * @param int    $default_id 默认选中的项
     */
    public function getHtmlTree($site_id = 1, $default_id = 0, $name = 'parent_id')
    {
        $dpTree = new dpTree(array('cat_id', 'parent_id'));
        $tree  = $dpTree ->getTree($this->getCats($site_id));
        $html  = '<select class="html_select" name="'.$name.'">';
        $html .= '<option value="0">≡ 为顶级栏目 ≡</option>';
        if (!empty($tree)) {
            foreach ($tree as $t) {
                if ($default_id == $t['cat_id'])
                    $attr = 'selected="selected"';
                else
                    $attr = '';
                $html .= '<option value="'.$t['cat_id'].'" '.$attr.'>'.$t['sname'].'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }

    /**
     * 获取分类树
     */
    public function getTree($site_id = 1, $icon)
    {
        $dpTree = new dpTree(array('cat_id', 'parent_id'));
        $dpTree->setIcon($icon);
        return $dpTree ->getTree($this->getCats($site_id));
    }

}
?>